<?php

class ContactAction extends PublicAction {





    protected  $db,$fields;





    public function _initialize()


    {


        parent::_initialize();





        defined('THEME_PATH') or define('THEME_PATH', TMPL_PATH.GROUP_NAME.'/'.C('DEFAULT_THEME').'/');





        $this->db = D(MODULE_NAME);





        $fields = F($this->moduleid.'_Field');





        foreach($fields as $key => $res){


            if ($res['status']==1) {


                $res['setup'] = string2array($res['setup']);


                $this->fields[$key] = $res;


            }


        }





        unset($fields);


        unset($res);


        $this->assign('fields',$this->fields);


        $this->assign('module',$this->module);


    }





    /**


     * 列表


     *


     */



    public function add()


    {


        if (IS_POST) {


            $model = $this->db;

            $fields = $this->fields ;

            $post = $_POST;


            $_POST = checkfield($fields,$post);

            if(empty($_POST)){


                $this->ajaxReturn(L('do_empty'));


            }

            $_POST['createtime'] = time();


            $_POST['updatetime'] = $_POST['createtime'];


            $_POST['userid'] = $module ? $userid : $_SESSION['userid'];


            $_POST['username'] = $module ? $username : $_SESSION['username'];


            if($_POST['style_color']){


                $_POST['style_color'] = 'color:'.$_POST['style_color'];


            }


            if($_POST['style_bold']){


                $_POST['style_bold'] = ';font-weight:'.$_POST['style_bold'];


            }





            if($_POST['style_color'] || $_POST['style_bold'] ){


                $_POST['title_style'] = $_POST['style_color'].$_POST['style_bold'];


            }





            $module = $module? $module : MODULE_NAME ;





            if (false === $model->create()) {


                $this->error($model->getError());


            }





            $_POST['id'] = $id= $model->add();





            if ($id !==false) {





                $catid = $_POST['catid'];





                if($_POST['aid']) {





                    $Attachment = M('attachment');





                    $aids = implode(',',$_POST['aid']);


                    $data['id']=$id;


                    $data['catid']= $catid;


                    $data['status']= '1';


                    $Attachment->where("aid in (".$aids.")")->save($data);


                }








                $data='';


                $cat = $this->categorys[$catid];





                //文章urls


                $data='';


                $cat = $this->categorys[$catid];


                if($cat['catdir']=="cye"||$cat['catdir']=="dongtai"||$cat['catdir']=="meiti"||$cat['catdir']=="wenti"


                    ||$cat['catdir']=="zc"||$cat['catdir']=="pyq"||$cat['catdir']=="xyk"||$cat['catdir']=="zs"||$cat['catdir']=="item"


                    ||$cat['catdir']=="gushi"||$cat['catdir']=="dxs"||$cat['catdir']=="pl"||$cat['catdir']=="youqian"||$cat['catdir']=="youqiananli"){


                    $url['0'] = "/article/".$id.".html";  //文章urle


                } else{


                    $url = geturl($cat,$_POST,$this->Urlrule);


                }





                $data['id']= $id;


                $data['url']= $url[0];





                $model->save($data);





                if($_POST['keywords'] && $module !='Page'){


                    $keywordsarr=explode(',',$_POST['keywords']);


                    $i=0;





                    $tagsdata =M('Tags_data');


                    $tagsdata->where("id=".$id)->delete();





                    foreach((array)$keywordsarr as $tagname){





                        if($tagname){





                            $tagidarr=$tagdatas=$where=array();


                            $where['name']=array('eq',$tagname);


                            $where['moduleid']=array('eq',$cat['moduleid']);


                            $tagid=M('Tags')->where($where)->field('id')->find();


                            $tagidarr['id']=$id;





                            if($tagid){


                                $num = $tagsdata->where("tagid=".$tagid[id])->count();


                                $tagdatas['num']=$num+1;


                                M('Tags')->where("id=".$tagid[id])->save($tagdatas);


                                $tagidarr['tagid']=$tagid['id'];


                            }else{


                                $tagdatas['moduleid']=$cat['moduleid'];


                                $tagdatas['name'] = $tagname;


                                $tagdatas['slug'] = Pinyin($tagname);


                                $tagdatas['num']=1;


                                $tagdatas['lang']=$_POST['lang'];


                                $tagdatas['module']= $cat['module'];


                                $tagidarr['tagid']=M('Tags')->add($tagdatas);


                            }


                            $i++;


                            $tagsdata->add($tagidarr);


                        }


                    }


                }





                if($cat['presentpoint']){


                    $user =M('User');


                    if($cat['presentpoint']>0)


                        $user->where("id=".$_POST['userid'])->setInc('point',$cat['presentpoint']);





                    if($cat['presentpoint']<0)


                        $user->where("id=".$_POST['userid'])->setDec('point',$cat['presentpoint']);





                }





                if($cat['ishtml'] && $_POST['status']){





                    if($module!='page' && $_POST['status'])


                        $this->create_show($id,$module);





                    if($this->sysConfig['HOME_ISHTML'])


                        $this->create_index();





                    $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));





                    foreach($arrparentid as $catid) {


                        if($this->categorys[$catid]['ishtml'])  $this->clisthtml($catid);


                    }


                }





                $this->assign('jumpUrl', U($module.'/index') );





                $this->success('新增成功！');


            } else {


                $this->error(L('add_error').': '.$model->getDbError());


            }


        } else {


            $form = new Form();





            $this->assign('form', $form );





            $template = file_exists(THEME_PATH.MODULE_NAME.'/edit.html') ? MODULE_NAME.':edit' : 'Content:edit';





            $this->display($template);


        }








    }








    public function edit()


    {


        if (IS_POST) {



            $model = $this->db;





            $fields = $this->fields ;




            if($fields['verifyCode']['status'] && (md5($_POST['verifyCode']) != $_SESSION['verify'])){





                $this->assign('jumpUrl','javascript:history.go(-1);');





                $this->error(L('error_verify'));





            }


            $post = $_POST;


            $_POST = checkfield($fields,$post);



            if(!array_key_exists('posid',$_POST))


                $_POST['posid']=0;





            if(empty($_POST)) $this->error (L('do_empty'));





            $_POST['updatetime'] = time();





            if($_POST['style_color'])


                $_POST['style_color'] = 'color:'.$_POST['style_color'];





            if($_POST['style_bold'])


                $_POST['style_bold'] =  ';font-weight:'.$_POST['style_bold'];





            if($_POST['style_color'] || $_POST['style_bold'] )


                $_POST['title_style'] = $_POST['style_color'].$_POST['style_bold'];








            $cat = $this->categorys[$_POST['catid']];





            $module = $module? $module : MODULE_NAME ;


//200421s


            $cate=$cat;


            if($cate['catdir']=="cye"||$cate['catdir']=="dongtai"||$cate['catdir']=="meiti"||$cate['catdir']=="wenti"


                ||$cate['catdir']=="zc"||$cate['catdir']=="pyq"||$cate['catdir']=="xyk"||$cate['catdir']=="zs"||$cate['catdir']=="item"


                ||$cate['catdir']=="gushi"||$cate['catdir']=="dxs"||$cat['catdir']=="pl"||$cat['catdir']=="youqian"||$cat['catdir']=="youqiananli"){


                $_POST['url'] = "/article/".$_POST['id'].".html";//文章urle


            }else{


                $_POST['url'] = geturl($cat,$_POST,$this->Urlrule);


                $_POST['url'] =$_POST['url'][0];


            }


            //$_POST['url'] = geturl($cat,$_POST,$this->Urlrule);


            //$_POST['url'] =$_POST['url'][0];


//200421e





            if (false === $model->create()) {


                $this->error ( $model->getError () );


            }





            // 更新数据


            $list=$model->save();





            if (false !== $list) {





                $id= $_POST['id'];




                $catid = $module =='page' ? $id : $_POST['catid'];





                if ($_POST['keywords']  && $module !='page') {





                    $keywordsarr=explode(',',$_POST['keywords']);





                    $i=0;





                    $tagsdata = M('Tags_data');





                    $tagsdata->where("id=".$id)->delete();





                    foreach ((array)$keywordsarr as $tagname) {





                        if ($tagname) {





                            $tagidarr=$tagdatas=$where=array();


                            $where['name']=array('eq',$tagname);


                            $where['moduleid']=array('eq',$cat['moduleid']);


                            $tagid=M('Tags')->where($where)->field('id')->find();


                            $tagidarr['id']=$id;





                            if ($tagid['id']>0) {


                                $num = $tagsdata->where("tagid=".$tagid[id])->count();


                                $tagdatas['num']=$num+1;


                                M('Tags')->where("id=".$tagid[id])->save($tagdatas);


                                $tagidarr['tagid']=$tagid['id'];


                            } else {


                                $tagdatas['moduleid']=$cat['moduleid'];


                                $tagdatas['name'] = $tagname;


                                $tagdatas['slug'] = Pinyin($tagname);


                                $tagdatas['num']=1;


                                $tagdatas['lang']=$_POST['lang'];


                                $tagdatas['module']= $cat['module'];


                                $tagidarr['tagid']=M('Tags')->add($tagdatas);


                            }





                            $i++;


                            $tagsdata->add($tagidarr);


                        }


                    }


                }





                if($_POST['aid']) {





                    $Attachment =M('attachment');





                    $aids =  implode(',',$_POST['aid']);





                    $data['id']= $id;


                    $data['catid']= $catid;


                    $data['status']= '1';


                    $Attachment->where("aid in (".$aids.")")->save($data);


                }





                $cat = $this->categorys[$catid];





                if($cat['ishtml']){





                    if($module!='page'  && $_POST['status'])  $this->create_show($_POST['id'],$module);





                    if($this->sysConfig['HOME_ISHTML']) $this->create_index();





                    $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));





                    foreach ($arrparentid as $catid) {





                        if ($this->categorys[$catid]['ishtml'])  $this->clisthtml($catid);


                    }


                }





                $this->success(L('edit_ok'));





            } else {


                //错误提示


                $this->error(L('edit_error').': '.$model->getDbError());


            }


        } else {



            $vo = $this->db->getById(1);





            if(MODULE_NAME == 'Resume'||MODULE_NAME =='Feedback'){


                $data['status']=1;


                $this->db->where('id='.$id)->save($data);


            }





            $vo['content'] = htmlspecialchars($vo['content']);





            $form = new Form($vo);





            $this->assign($_REQUEST);


            $this->assign('vo', $vo);


            $this->assign('form', $form);



            $template = file_exists(THEME_PATH.MODULE_NAME.'/edit.html') ? MODULE_NAME.':edit' : 'Content:edit';


            $this->display($template);


        }


    }








    // 空模块的ajax内容录入


    public function ajax_content_insert()


    {


        $id=intval(trim($_POST['id']));





        $data=$this->db->find($id);





        if($data){





            $_POST['updatetime'] = time();


            unset($_POST['id']);


            $sres=$this->db->save($_POST,array('where'=>'id="'.$id.'"'));


            $result['issu']=$sres? '更新成功' : '更新失败';





        }else{


            $_POST['createtime']=time();


            $_POST['status']=1;


            $_POST['lang']=LANG_ID;


            $r=$this->db->add($_POST);


            $result['issu']=$r? L('add_ok') : L('add_error');


        }





        echo json_encode($result);


        exit;


    }





    function statusallok()


    {


        $module = MODULE_NAME;





        $model = M($module);





        $ids = $_POST['ids'];





        if (!empty($ids) && is_array($ids)) {





            $id = implode(',',$ids);





            $data = $model->select($id);





            if($data){





                foreach($data as $key=>$r){





                    $model->save(array(id=>$r['id'],status=>1));





                    if($this->categorys[$r['catid']]['ishtml'] && $r['status'])


                        $this->create_show($r['id'],$module);





                }





                $cat = $this->categorys[$r['catid']];





                if($cat['ishtml']){





                    if($this->sysConfig['HOME_ISHTML'])


                        $this->create_index();





                    $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));





                    foreach($arrparentid as $catid) {





                        if($this->categorys[$catid]['ishtml'])


                            $this->clisthtml($catid);





                    }





                }


                $this->success(L('do_ok'));


            }else{


                $this->error(L('do_error').': '.$model->getDbError());


            }


        }else{


            $this->error(L('do_empty'));


        }


    }





    public function xiugai()


    {


        $model = D (MODULE_NAME);


        $idss = $_POST['ids'];





        if(!empty($idss) && is_array($idss)){





            $idArr = implode(',',$idss);


            $where = "id in(".$idArr.")";





            $data['status']= '1';





            if(!empty($_POST['posid'])){


                $data['posid']= intval($_POST['posid']);


            }





            if (!empty($_POST['catid'])) {





                $list = $model->field('id,catid,url')->where($where)->select();





                foreach ($list as $r) {


                    //if($r['islink']) continue;





                    $r['catid']= intval($_POST['catid']);


                    $url = geturl($this->categorys[$r['catid']],$r,$this->Urlrule);





                    $r['url'] = $url['0'];





                    $model->save($r);


                }


            } else {


                $model->where($where)->data($data)->save();





                $this->success (L('do_eds'));


            }





        } else {


            $this->success(L('do_ed'));


        }


        if(in_array($name,$this->cache_model)) savecache($name);


        $this->success(L('do_ok'));


    }








    /*状态*/


    public function status()


    {





        $module = MODULE_NAME;





        $model = D($module);





        if($model->save($_GET)){





            $_POST ='';





            $_POST = $model->find($_GET['id']);





            $cat =  $this->categorys[$_POST['catid']];





            if($cat['ishtml']){





                if($module!='Page' && $_POST['status'])


                    $this->create_show($_POST['id'],$module);





                if($this->sysConfig['HOME_ISHTML']) $this->create_index();





                $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));





                foreach($arrparentid as $catid) {





                    if($this->categorys[$catid]['ishtml'])  $this->clisthtml($catid);





                }





            }





            $this->success(L('do_ok'));





        }else{


            $this->error(L('do_error'));


        }


    }


}